﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;

public partial class DepSetings : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if(Session["userName"]!=null)
        {
           if(!IsPostBack)
          {
            BindUsers();
            AddDialogBox();
           }
        }
        else
        {
            Response.Redirect(GetUrlPath.GetUrl + "/Login.aspx");
        }

    }
    protected void tbnAddDep_Click(object sender, EventArgs e)
    {
        panelAdd.Visible = true;
        panelChange.Visible = false;
        panelDelDep.Visible = false;
    }

    protected void btnChangeMsg_Click(object sender, EventArgs e)
    {
        panelAdd.Visible = false;
        panelChange.Visible = true;
        panelDelDep.Visible = false;
        //绑定系统用户下拉列表
        string strFindUsers = "select TRUENAME, USERNAME from USERS";
        DBOperation db = new DBOperation();
        SqlDataReader sdr = db.GetDataReader(strFindUsers);
        ddlChangeMan.DataSource = sdr;
        ddlChangeMan.DataTextField = "TRUENAME";
        ddlChangeDep.DataValueField = "USERNAME";
        ddlChangeMan.DataBind();
        
        //绑定系统部门
        string strFindDep = "select DEPID ,DEPNAME from DEPARTMENT where DEPNAME<>'生技部'";
        sdr = db.GetDataReader(strFindDep);
        ddlChangeDep.DataSource = sdr;
        ddlChangeDep.DataTextField = "DEPNAME";
        ddlChangeDep.DataValueField = "DEPID";
        ddlChangeDep.DataBind();
        sdr.Close();

    }
    //删除事件
    protected void btnDelDep_Click(object sender, EventArgs e)
    {
        panelAdd.Visible = false;
        panelChange.Visible = false;
        panelDelDep.Visible = true;
        BindDelddL();
     

    }
    //添加部门
    protected void btnAddDep_Click(object sender, EventArgs e)
    {
        if(Page.IsValid)
        {
            Department dep = new Department();
            dep.DepName = txtAddDepName.Text.Trim();
            dep.PrincipaID = ddlAddMan.SelectedValue.ToString();
            dep.Brief = txtAddBrief.Text.Trim();
            dep.Status = 1;
            if (dep.AddDepartment())
            {
                Response.Write("<script language='javascript'>alert('添加成功！')</script>");
            }
            else 
            {
                Response.Write("<script language='javascrip'>alert('添加失败！')</script>");
            }

        }
       
    }
    protected void rbtnNo_CheckedChanged(object sender, EventArgs e)
    {
        PanelDialog.Visible = true;
        PanelChangDep.Visible = false;
    }
    protected void rbtnYes_CheckedChanged(object sender, EventArgs e)
    {
        PanelDialog.Visible = false;
        PanelChangDep.Visible = true;
    }
    //服务端验证，查看将要添加的部门是否已经存在
    protected void CustomValidDep_ServerValidate(object source, ServerValidateEventArgs args)
    {
        string depName = args.Value.ToString();
        DataRow dr= Department.searchDep(depName);
        if (dr != null)
        {
            args.IsValid = false;

        }
        else
        {
            args.IsValid = true;
        }
    }
    //读取系统用户绑定到用户下拉列表
    protected void BindUsers()
    {
        string strFindUsers = "select TRUENAME, USERNAME from USERS";
        DBOperation db = new DBOperation();
        SqlDataReader sdr = db.GetDataReader(strFindUsers);
        ddlAddMan.DataSource = sdr;
        ddlAddMan.DataTextField = "TRUENAME";
        ddlAddMan.DataValueField = "USERNAME";
        ddlAddMan.DataBind();
        sdr.Close();
 
    }
    //修改部门方法
    protected void btnSure_Click(object sender, EventArgs e)
    {
        if(Page.IsValid)
        {
        Department dep = new Department();
        dep.DepName = txtChangeDep.Text.Trim();
        dep.PrincipaID = ddlChangeMan.SelectedValue.ToString();
        dep.Status = 1;
        dep.Brief = txtChangeBrief.Text.Trim();
        if(dep.AlterDepartment(Convert.ToInt32( ddlChangeDep.SelectedValue)))
        {
            Response.Write("<script language='javascript'>alert('修改成功！')</script>");
           
        }
        else
        {
            Response.Write("<script language='javascrip'>alert('修改失败！')</script>");
        }
        }
    }
    //为修改事件，删除事件添加客户端确认对话框
    protected void AddDialogBox()
    {
        btnSure.Attributes.Add("onClick","return confirm('您真的要修改该部门的信息吗？');");
        btnDel.Attributes.Add("onClick","return confirm('您真的要删除该部门吗？');");
    }
   
    /// <summary>
    /// 绑定删除功能模块的DDlleftdep下拉列表
    /// </summary>
    /// <param name="depID">被选择的部门的ID</param>
    protected void BindDDlLeftDep(int depID)
    {
        string strFindLeftDep = "select DEPID,DEPNAME from DEPARTMENT where DEPID<>'"+depID+"'";
        DBOperation db = new DBOperation();
        SqlDataReader sdr = db.GetDataReader(strFindLeftDep);
        ddlLeftDep.DataSource = sdr;
        ddlLeftDep.DataTextField = "DEPNAME";
        ddlLeftDep.DataValueField = "DEPID";
        ddlLeftDep.DataBind();
        sdr.Close();
    }
    //绑定删除的下拉列表
    protected void BindDelddL()
    {
        string strFindDep = "select DEPID,DEPNAME from DEPARTMENT where DEPNAME<>'生技部'";
        DBOperation db = new DBOperation();
        SqlDataReader sdr = db.GetDataReader(strFindDep);
        ddlDelDep.DataSource = sdr;
        ddlDelDep.DataTextField = "DEPNAME";
        ddlDelDep.DataValueField = "DEPID";
        ddlDelDep.DataBind();
        sdr.Close();
        if (ddlDelDep.SelectedValue!="")
        {
        BindDDlLeftDep(Convert.ToInt32(ddlDelDep.SelectedValue));
        }
    }

    protected void ddlDelDep_SelectedIndexChanged(object sender, EventArgs e)
    {
        BindDDlLeftDep(Convert.ToInt32(ddlDelDep.SelectedValue));
    }
    protected void btnDel_Click(object sender, EventArgs e)
    {
        //删除部门，保留用户
        if(rbtnYes.Checked)
        {
            if (Department.DelDepSaveUser(Convert.ToInt32(ddlDelDep.SelectedValue), Convert.ToInt32(ddlLeftDep.SelectedValue)))
            {
                Response.Write("<script language='javascript'>alert('删除成功！')</script>");
                BindDelddL();
            }
            else
            {
                Response.Write("<script language='javascript'>alert('在试图进行数据库删除操作时，发生了意外，删除失败！请联系管理员解决这个问题。')</script>");
            }
        }
        //删除部门，删除用户，保留信息
        if(rbtnNo.Checked&&rBtnSaveMsg.Checked)
        {
            if(Department.DelDepDelUserSaveMsg(Convert.ToInt32( ddlDelDep.SelectedValue)))
            {
                 Response.Write("<script language='javascript'>alert('删除成功！')</script>");
                 BindDelddL();
            }
            else
            {
                Response.Write("<script language='javascript'>alert('在试图进行数据库删除操作时，发生了意外，删除失败！请联系管理员解决这个问题。')</script>");
            }
        }
        //删除部门、用户、信息
        if(rbtnNo.Checked&&rBtnNoSaveMsg.Checked)
        {
            if(Department.DelAll(Convert.ToInt32(ddlDelDep.SelectedValue)))
            {
                Response.Write("<script language='javascript'>alert('删除成功！')</script>");
                BindDelddL();
            }
            else
            {
                Response.Write("<script language='javascript'>alert('在试图进行数据库删除操作时，发生了意外，删除失败！请联系管理员解决这个问题。')</script>");
            }

        }
        
    }
}
